package com.wlient.demo.service.mqtt;


import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * MQTT工具类操作
 *
 * @author Mr.Qu
 * @since v1.1.0 2020-01-10
 */
@Slf4j
@Service
public class MqttConnectService {

    @Resource
    public MqttClient mqttClient;

    /**
     * 客户端connect连接mqtt服务器
     *
     * @param userName     用户名
     * @param passWord     密码
     * @param mqttCallback 回调函数
     **/
//    public void setMqttClient(String userName, String passWord, MqttCallback mqttCallback) throws MqttException {
//        MqttConnectOptions options = ;
//        if (mqttCallback == null) {
//            mqttClient.setCallback(new Callback());
//        } else {
//            mqttClient.setCallback(mqttCallback);
//        }
//        mqttClient.connect(options);
//    }

    /**
     * 关闭MQTT连接
     */
    public void close() throws MqttException {
        mqttClient.close();
        mqttClient.disconnect();
    }

    /**
     * 向某个主题发布消息 默认qos：1
     *
     * @param topic:发布的主题
     * @param msg：发布的消息
     */
    public void pub(String topic, String msg) throws MqttException {
        MqttMessage mqttMessage = new MqttMessage();
        //mqttMessage.setQos(2);
        mqttMessage.setPayload(msg.getBytes());
        MqttTopic mqttTopic = mqttClient.getTopic(topic);
        MqttDeliveryToken token = mqttTopic.publish(mqttMessage);
        token.waitForCompletion();
    }

    /**
     * 向某个主题发布消息
     *
     * @param topic: 发布的主题
     * @param msg:   发布的消息
     * @param qos:   消息质量    Qos：0、1、2
     */
    public void pub(String topic, String msg, int qos) throws MqttException {
        MqttMessage mqttMessage = new MqttMessage();
        mqttMessage.setQos(qos);
        mqttMessage.setPayload(msg.getBytes());
        MqttTopic mqttTopic = mqttClient.getTopic(topic);
        MqttDeliveryToken token = mqttTopic.publish(mqttMessage);
        token.waitForCompletion();
    }

    /**
     * 订阅某一个主题 ，此方法默认的的Qos等级为：1
     *
     * @param topic 主题
     */
    public void sub(String topic) throws MqttException {
        mqttClient.subscribe(topic);
    }

    /**
     * 订阅某一个主题，可携带Qos
     *
     * @param topic 所要订阅的主题
     * @param qos   消息质量：0、1、2
     */
    public void sub(String topic, int qos) throws MqttException {
        mqttClient.subscribe(topic, qos);
    }
//    /**
//     * main函数自己测试用
//     */
//    public static void main(String[] args) throws MqttException {
//        try {
//            MQTTConnect mqttConnect = new MQTTConnect();
//            mqttConnect.setMqttClient("admin", "public", new Callback());
//            //主题命名最好是分传感器模块，不同的模块绑定不同的主题。
//            mqttConnect.sub("com/iot/init", 2);
//            new Thread() {
//                @Override
//                public void run() {
//                    int i = 1;
//                    while (true) {
//                        try {
//                            mqttConnect.pub("com/iot/init", "熊哥你好！*" + i);
//                            i++;
//                            sleep(1000);
//                        } catch (MqttException | InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }.start();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
}